package org.abettor.pushbox.map;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.abettor.pushbox.download.DownloadMap;
import org.abettor.pushbox.view.BoxMapThumbnailDraw;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;

/**
 * 读取网络地图并生成缩略图的操作
 * 
 * @author jzh
 * 
 */
public abstract class AsyncGenerateNetMapThumbImage {
	// 进行回调的地图的方法
	public static interface NetImageCallBack {
		public void processImage(Bitmap bit);
	}
	//进行回调的方法
	private final Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg){
			callback.processImage(bitmap);
		}
	};
	//产生缩略图的操作
	protected Bitmap generateImage(){
		//判断缩略图的文件是否存在,存在的话直接读取文件
		String thrumb = getThumbNailPath() + File.separator + getNetMapId()+".thrumb";
		Bitmap bmp = null;
		File file = new File(thrumb);
		if(file.exists()){//读取存在的文件
			bmp = readBitmap(file);
		}
		if(bmp != null){
			return bmp;
		}
		byte [] buf = null;
		try{
			buf = readFile();
		}catch(Exception e){
		}
		if(buf==null){//数据为空,从网络上读取
			buf = readMapFromNet();
		}
		if(buf==null){
			return null;
		}
		Boxmap map = BoxmapUtil.paraArrayToMap(buf);//生成地图
		mapThumbnailDraw.setMap(map);
		bmp = mapThumbnailDraw.draw();
		//在本地缓存一份
		generateThumbnail(bmp,thrumb);
		return bmp;
	}
	//在sd卡上存一个缩略图
	protected void generateThumbnail(Bitmap bmp,String filepath){
		FileOutputStream os;
		try {
			os = new FileOutputStream(filepath);
			bmp.compress(Bitmap.CompressFormat.PNG, 100, os);
			os.close();
		} catch (IOException e) {
		}
	}
	//从文件读取图像
	protected Bitmap readBitmap(File file){
		FileInputStream is;
		try {
			is = new FileInputStream(file);
			Bitmap bitmap = BitmapFactory.decodeStream(is);
			return bitmap;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	//读取文件
	protected byte [] readFile() throws IOException{
		String filePath = getMapFilePath();
		File file = new File(filePath+File.separator+getNetMapId()+".map");
		if(!file.exists()){
			return null;
		}
		InputStream in = new FileInputStream(file);
		ByteArrayOutputStream byteout = new ByteArrayOutputStream();
		int value = 0;
		while((value = in.read())!=-1){
			byteout.write(value);
		}
		return byteout.toByteArray();
	}
	private NetImageCallBack callback;// 回调的接口
	protected Bitmap bitmap;
	protected BoxMapThumbnailDraw mapThumbnailDraw;// 缩略图的绘制类
	private DownloadMap downBean;// 下载地图的beanFileInputStream is;

	public abstract int getNetMapId();// 获得网络地图的id

	public abstract String getMapFilePath();// 获得要保存的地图的id
	public abstract String getThumbNailPath();//获得缩略图的路径

	public AsyncGenerateNetMapThumbImage(float width, float height,
			NetImageCallBack callback, Activity context) {
		this.callback = callback;
		this.mapThumbnailDraw = new BoxMapThumbnailDraw(context);
		this.mapThumbnailDraw.setHeight(height);
		this.mapThumbnailDraw.setWidth(width);
		downBean = new DownloadMap(context);
	}

	// 从网络上读取地图的二进制数据
	protected byte [] readMapFromNet(){
		int id = getNetMapId();//从网络读取id
		byte [] buf = downBean.readMapFromNet(id);
		if(buf != null){
			String fileName = getMapFilePath() + File.separator +  getNetMapId() + ".map";
			writeFile(buf,fileName);//把地图在本地缓存
		}
		return buf;
	}
	// 写入文件
	private void writeFile(byte[] buf, String fileName) {
		FileOutputStream os;
		try {
			os = new FileOutputStream(fileName);
			os.write(buf);
			os.close();
		} catch (Exception e) {
		}
	}
	/**
	 * 开始执行操作
	 */
	public void start(){
		new Thread(){
			@Override
			public void run(){
				bitmap = generateImage();
				handler.sendEmptyMessage(0);
			}
		}.start();
	}
}
